package com.shu.config;

import com.shu.shiro.CustomAuthorizingRealm;
import com.shu.shiro.CustomCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.Cookie;
import org.apache.shiro.web.servlet.ShiroHttpSession;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 暂不用
 * Created by shu on 2017/8/16.
 */

public class ShiroConfig {

    @Value("${shiro.remember.key}")
    private String key;

    @Bean
    public CustomAuthorizingRealm customAuthorizingRealm(CustomCredentialsMatcher customCredentialsMatcher){
        CustomAuthorizingRealm customAuthorizingRealm = new CustomAuthorizingRealm();
        customAuthorizingRealm.setCredentialsMatcher(customCredentialsMatcher);
        return  customAuthorizingRealm;
    }

    @Bean
    public CustomCredentialsMatcher customCredentialsMatcher(){
        CustomCredentialsMatcher customCredentialsMatcher = new CustomCredentialsMatcher();
        return customCredentialsMatcher;
    }

    @Bean
    public DefaultWebSecurityManager webSecurityManager(CustomAuthorizingRealm customAuthorizingRealm, CookieRememberMeManager cookieRememberMeManager,
                                                        DefaultWebSessionManager defaultWebSessionManager, EhCacheManager ehCacheManager){

        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();

        securityManager.setRealm(customAuthorizingRealm);

        securityManager.setSessionManager(defaultWebSessionManager);

        securityManager.setCacheManager(ehCacheManager);

        securityManager.setRememberMeManager(cookieRememberMeManager);

        return securityManager;

    }


    @Bean
    public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);

        /**
         * 默认的登陆访问url
         */
        shiroFilter.setLoginUrl("/login");
        /**
         * 登陆成功后跳转的url
         */
        shiroFilter.setSuccessUrl("/");
        /**
         * 没有权限跳转的url
         */
        shiroFilter.setUnauthorizedUrl("/global/error");


        //拦截器.
        Map<String,String> filterChainDefinitionMap = new LinkedHashMap<String,String>();


        filterChainDefinitionMap.put("/**/login", "anon");
        filterChainDefinitionMap.put("/**/logout", "logout");
        filterChainDefinitionMap.put("/member/reg", "anon");
        filterChainDefinitionMap.put("/demo/user", "user");
        filterChainDefinitionMap.put("/demo/log", "authc");
        shiroFilter.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilter;
    }


    @Bean
    public EhCacheManager ehCacheManager(){
        EhCacheManager cacheManager = new EhCacheManager();
        cacheManager.setCacheManagerConfigFile("classpath:ehcache-shiro.xml");
        return cacheManager;
    }

    @Bean
    public CookieRememberMeManager rememberMeManager(SimpleCookie rememberMeCookie) {
        CookieRememberMeManager manager = new CookieRememberMeManager();
        manager.setCipherKey(Base64.decode(key));
        manager.setCookie(rememberMeCookie);
        return manager;
    }

    /**
     * 记住密码Cookie
     */
    @Bean
    public SimpleCookie rememberMeCookie() {
        SimpleCookie simpleCookie = new SimpleCookie("rememberMe");
        simpleCookie.setHttpOnly(true);
        simpleCookie.setMaxAge(7 * 24 * 60 * 60);//7天
        return simpleCookie;
    }


    @Bean
    public DefaultWebSessionManager defaultWebSessionManager(EhCacheManager ehCacheManager) {
        System.out.println("ShiroConfiguration.defaultWebSessionManager()");
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        //sessionManager.setSessionDAO(new CustomSessionDAO());
        sessionManager.setCacheManager(ehCacheManager);
        //单位为毫秒（1秒=1000毫秒） 3600000毫秒为1个小时
        sessionManager.setSessionValidationInterval(3600000*12);
        //3600000 milliseconds = 1 hour
        sessionManager.setGlobalSessionTimeout(3600000*12);
        sessionManager.setDeleteInvalidSessions(true);
        sessionManager.setSessionValidationSchedulerEnabled(true);
        Cookie cookie = new SimpleCookie(ShiroHttpSession.DEFAULT_SESSION_ID_NAME);
        cookie.setName("shiroCookie");
        cookie.setHttpOnly(true);
        sessionManager.setSessionIdCookie(cookie);
        return sessionManager;
    }


}
